using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace VOL.Core.Extensions
{
    /// <summary>
    /// Excel导出工具类
    /// </summary>
    public static class ExportExcel
    {
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="fileName">文件名</param>
        /// <returns>Excel文件的字节数组</returns>
        public static byte[] Export(List<Dictionary<string, object>> data, string fileName)
        {
            if (data == null || data.Count == 0)
            {
                throw new ArgumentException("数据不能为空");
            }

            // 创建工作簿
            XSSFWorkbook workbook = new XSSFWorkbook();
            try
            {
                // 创建表单
                ISheet sheet = workbook.CreateSheet(Path.GetFileNameWithoutExtension(fileName));
                
                // 创建表头行
                IRow headerRow = sheet.CreateRow(0);
                // 表头单元格样式
                ICellStyle headerStyle = workbook.CreateCellStyle();
                headerStyle.FillForegroundColor = IndexedColors.Grey25Percent.Index;
                headerStyle.FillPattern = FillPattern.SolidForeground;
                IFont font = workbook.CreateFont();
                font.Boldweight = (short)FontBoldWeight.Bold;
                headerStyle.SetFont(font);

                // 获取列名
                Dictionary<string, object> firstRow = data[0];
                int colIndex = 0;
                foreach (var key in firstRow.Keys)
                {
                    ICell cell = headerRow.CreateCell(colIndex++);
                    cell.SetCellValue(key);
                    cell.CellStyle = headerStyle;
                }

                // 添加数据行
                for (int i = 0; i < data.Count; i++)
                {
                    IRow dataRow = sheet.CreateRow(i + 1);
                    colIndex = 0;
                    foreach (var value in data[i].Values)
                    {
                        ICell cell = dataRow.CreateCell(colIndex++);
                        if (value == null)
                        {
                            cell.SetCellValue(string.Empty);
                        }
                        else if (value is DateTime)
                        {
                            cell.SetCellValue(((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"));
                        }
                        else
                        {
                            cell.SetCellValue(value.ToString());
                        }
                    }
                }

                // 自动调整列宽
                for (int i = 0; i < firstRow.Count; i++)
                {
                    sheet.AutoSizeColumn(i);
                }

                // 直接写入内存并返回字节数组
                using (MemoryStream ms = new MemoryStream())
                {
                    workbook.Write(ms);
                    return ms.ToArray();
                }
            }
            finally
            {
                // 确保释放资源
                workbook.Close();
            }
        }
    }
} 